package com.company.笔试.xml生成器;

import org.apache.commons.lang.StringUtils;

/**
 * @ClassName xmlBuilder
 * @Description TODO
 * @Author 11
 * @Date 2021/4/7 9:54
 * @Version 1.0
 */
public class xmlBuilder {


    private String version;
    private String encoding;
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
    public String getEncoding() {
        return encoding;
    }
    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }



    /**
     * 生成头文件
     */
    public String createXmlHeader(String version,String encoding){
        if (StringUtils.isEmpty(version)) return "";
        if (StringUtils.isEmpty(encoding)) return "";
        this.version = version;
        this.encoding = encoding;
        StringBuilder builder = new StringBuilder();
        builder.append("<?xml version=\"").append(this.version).append("\"")
                .append(" encoding=\"").append(this.encoding).append("\"?>")
                .append("\r\n");
        return builder.toString();
    }


    /**
     * 打印xml结构
     * @param node
     */
    public void printXmlInfo(String version,String encoding,Node node){
        String xml = this.createXmlHeader(version, encoding) + Node.createXml(node);
        System.out.println(xml);
    }

    public static void main(String[] args) {
        Node root = new Node();
        root.setId("").setName("CategoryList").setDescription("");

        Node node1 = new Node();
        node1.setParentNode(root).setName("Category").setId("01").setDescription("");
        root.setChildNode(node1);

        Node node2 = new Node();
        node2.setParentNode(node1).setName("MainCategory").setId("").setDescription("XML");
        node1.setChildNode(node2);

        Node node3 = new Node();
        node3.setParentNode(node1).setName("Description").setId("").setDescription("This is a list my XML articles.");
        node1.setChildNode(node3);

        Node node4 = new Node();
        node4.setParentNode(node1).setName("Active").setId("").setDescription("true");
        node1.setChildNode(node4);

//        Node node5 = new Node();
//        node5.setParentNode(node4).setId("01").setName("test").setDescription("test");
//        node4.setChildNode(node5);

//        Node.removeNode(node1);
//
//        root.printNodeInfo(null);

//        System.out.println(Node.createXml(root));

        xmlBuilder xmlBuilder = new xmlBuilder();
        xmlBuilder.printXmlInfo("1.0","UTF-8",root);

//        System.out.println("-----------------------------删除MainCategory节点后------------------------------------------------------");
//        Node.removeNode(node2);
//        xmlBuilder.printXmlInfo("1.0","UTF-8",root);
//
//        System.out.println("-----------------------------删除Category节点后------------------------------------------------------");
//        Node.removeNode(node1);
//        xmlBuilder.printXmlInfo("1.0","UTF-8",root);
    }

}
